// pages/index/free/decoloan.js
var app = getApp();
var call = require("../../../utils/request.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    city: '厦门',
    clickId: -1,
    clickIds: -1,
    rote: 0,
    money: 0,
    month: 0,
    ruturn: 0,
    sum: 0,
    card: '获取验证码',
    hidecode: true,
    showcode: false,
    distate: false,
    mobile: '',
    codesid:'',
    landing_page:'',
    btns: [{
        counts: [{
            rate: 0.08666,
            count: "10W",
            money: 100000
          },
          {
            rate: 0.08666,
            count: "20W",
            money: 200000
          },
          {
            rate: 0.08666,
            count: "30W",
            money: 300000
          }
        ]
      },
      {
        months: [{
            value: 12,
            month: "12个月"
          },
          {
            value: 24,
            month: "24个月"
          },
          {
            value: 36,
            month: "36个月"
          }
        ]
      }

    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    //that.session()
    call.getData('session', this.shuffleSuc, this.fail);
    //that.getsettion()
    setTimeout(function(){
      that.creditview()
    },200)
  },
  //获取session
  shuffleSuc: function (res) {
    console.log(res)
    var that = this;
    var sid = res.data.PHPSESSID
    wx.setStorage({ //存储到本地
      key: "sid",
      data: sid
    })
    that.setData({
      codesid: sid
    })

  },
  fail: function () {
    console.log("失败")
  },
  //初始化验证码
  creditview:function(){
    var that = this
    var codesid = that.data.codesid
    console.log("codesid为",codesid)
    var text = '获取验证码'
    wx.request({
      url: app.apiUrl('c=zhaobiao/index&a=view_credit'),
      data: {},
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'PHPSESSID=' + codesid
      },
      method: "GET",
      success: function (res) {
        console.log(res.data)
        var code = res.data.datas.code
        var landing_page = res.data.datas.landing_page
        var bind_count = res.data.datas.bid_count
        var s1 = bind_count.substring(0, 1);//取前半部分
        var s2 = bind_count.substring(1, 2);
        var s3 = bind_count.substring(2,3);
        var bids = bind_count.replace(/\b(0+)/gi, "")
        that.setData({
          card: code,
          hidecode: false,
          showcode: true,
          landing_page: landing_page,
          num1: s1,
          num2: s2,
          num3: s3,
          count:bids
        })
        setTimeout(function () {
          that.setData({
            card: text,
          })
        }, 30000)
      },
      fail: function (res) {
        console.log('请求失败', res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var animatione = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    this.animation = animatione
    var next = true;
    //连续动画关键步骤
    setInterval(function() {
      if (next) {
        this.animation.scale(0.95).step()
        next = !next;
      } else {
        this.animation.scale(1).step()
        next = !next;
      }
      this.setData({
        animationDatas: animatione.export()
      })
    }.bind(this), 500)
  },

  //金额点击选取
  chooseamount: function(e) {
    var that = this;
    var length = that.data.clickId;
    var index = parseInt(e.currentTarget.id);
    var rote = e.currentTarget.dataset.rotem
    var money = e.currentTarget.dataset.moneyh
    var month = that.data.month
    if(month==0){
      var ruturnm =0
      var sum =0
    }else{
      var ruturnm = (money / month * (1 + rote)).toFixed(2)
      var sum = (ruturnm * month).toFixed(2)
    }
    console.log()
    if (this.data.clickId == e.currentTarget.id) {
      this.setData({
        clickId: 1,
      })
      return;
    }
    that.setData({
      clickId: e.currentTarget.id,
      rote: rote,
      money: money,
      month: month,
      ruturn: ruturnm,
      sum: sum
    })

    console.log(e.currentTarget)
  },
  choosemonth: function(e) {
    var that = this;
    var length = that.data.clickIds;
    var index = parseInt(e.currentTarget.id);
    var rote = that.data.rote
    var money = that.data.money
    var month = e.currentTarget.dataset.month
    var ruturnm = (money / month * (1 + rote)).toFixed(2)
    var sum = (ruturnm * month).toFixed(2)
    var index = parseInt(e.currentTarget.id);
    if (this.data.clickIds == e.currentTarget.id) {
      this.setData({
        clickIds: 1,
      })
      return;
    }
    that.setData({
      clickIds: e.currentTarget.id,
      rote: rote,
      money: money,
      month: month,
      ruturn: ruturnm,
      sum: sum
    })
    console.log(e)
  },
  skipCalcu: function(e) {
    wx.pageScrollTo({
      scrollTop: 800
    })
  },
  skipTop: function(e) {
    wx.pageScrollTo({
      scrollTop: 0
    })
  },
  //按钮输入
  bindKeyInput: function(e) {
    var value = e.detail.value
    this.setData({
      inputValue: e.detail.value,
      modelclear: true,
      disableds: false,
      start: true,
      hidecode: false,
      showcode: true,
      distate: false,
      mobile: value
    })
  },
  settime: function () {
    var that = this
    setTimeout(function () {
      that.setData({
        alertmess: false
      })
    }, 2000)
  },
  //获取验证码
  getCode: function(e) {
    var that = this
    var mobile = that.data.mobile
    var codesid = that.data.codesid
    console.log('阿达',codesid)
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var text = '获取验证码'
    var card = that.data.card
    if (mobile == '') {
      that.setData({
        alertmess: true,
        message: '请输入手机号码！'
      })
      that.settime()
    } else if (mobile.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(mobile)) {
      that.setData({
        alertmess: true,
        message: '手机号码错误,请重新输入！'
      })
      that.settime()
    }else{
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=get_captcha'),
        data: {},
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + codesid
        },
        method: "GET",
        success: function (res) {
          console.log(res.data)
          var cards = res.data.datas.code
          console.log(cards)
          that.setData({
            card: cards,
            distate: false
          })
          setTimeout(function () {
            that.setData({
              card: text,
              distate: false
            })
          }, 60000)
        },
        fail: function (res) {
          console.log('请求失败', res.data)
        }
      })
    } 
  
  },
  //form表单跳转提交
  formSubmit: function(e) {
    var that = this
    var city = e.detail.value.city
    var aleaname = e.detail.value.rqname
    var area = e.detail.value.area
    var tele = e.detail.value.tele
    var card = e.detail.value.card
    var thecard = that.data.card  
    var codesid = that.data.codesid
    var landing_page = that.data.landing_page
    var id = 2
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    var title = '申请资料提交成功'
    var tsmall = '恭喜您，申请资料成功，我们将尽快与您联系!'
    if (tele == '') {
      that.setData({
        alertmess: true,
        message: '手机号码不能为空！'
      })
      that.settime()
    } else if (tele.length != 11) {
      that.setData({
        alertmess: true,
        message: '请重新输入手机号码！'
      })
      that.settime()
    } else if (!myreg.test(tele)) {
      that.setData({
        alertmess: true,
        message: '手机号错误，请重新输入！'
      })
      that.settime()
    }else if (card == '') {
      that.setData({
        alertmess: true,
        message: '验证码不能为空！'
      })
      that.settime()
    } else if (card !=thecard){
      that.setData({
        alertmess: true,
        message: '验证码不对，请重新输入！'
      })
      that.settime()
    }
    else {
      wx.request({
        url: app.apiUrl('c=zhaobiao/index&a=submit_credit'),
        data: {
          phone: tele,
          landing_page:landing_page,
          code:card
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': 'PHPSESSID=' + codesid
        },
        method: "GET",
        success: function(res) {
          console.log(res.data)
          var code = res.data.code
          var message = res.data.error
          if (code == 200) {
            wx: wx.navigateTo({
              url: 'applysuccess?data=' + [id, title, tsmall],
            })
          } else if(code==400){
            that.setData({
              alertmess: true,
              message: message
            })
            that.settime()
          }   
        },
        fail: function(res) {
          console.log('请求失败', res.data)
        }
      })

    }
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
  },
})